import java.util.List;

/*
 * @author zzr
 * @date: 2025/10/12  15:14
 * @description: 给一个单链表的头结点 head，返回链表的中间节点
 * 如果有两个中间节点，则返回第二个中间节点
 */
public class Demo5 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public ListNode middleNode(ListNode head) {

        ListNode fast = head;
        ListNode slow = head;

        // 判断条件一定是 fast != null 在前面，否则会出现空指针异常
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}
